package com.service.device;

import com.common.ErrorCodeEnum;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.DateUtils;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;

import com.dao.primary.user.UserInfoMapper;
import com.dao.secondary.device.DeviceObdExFactoryBatchInfoMapper;
import com.model.domain.device.GetDeviceObdBaseInfoDO;
import com.model.domain.device.GetDeviceObdExFactoryBatchInfoDO;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.pojo.device.DeviceObdExFactoryBatchInfo;
import com.model.pojo.user.UserInfo;
import com.service.user.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @Author: zql
 * @Description: OBD设备出厂批次管理服务
 * @Data: 2023-07-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceObdExFactoryBatchInfoService {
    private final DeviceObdExFactoryBatchInfoMapper deviceObdExFactoryBatchInfoMapper;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    private final UserInfoService userInfoService;
    /**
     * 分页数据
     *
     * @param getDeviceObdExFactoryBatchInfoDO
     * @return List<DeviceObdExFactoryBatchInfo>
     * @author tm
     * @date 2023-07-10 22:36
     */
    public List<DeviceObdExFactoryBatchInfo> getPageList(GetDeviceObdExFactoryBatchInfoDO getDeviceObdExFactoryBatchInfoDO) {
        PageHelper.clearPage();
        PageHelper.startPage(getDeviceObdExFactoryBatchInfoDO.getPage(), getDeviceObdExFactoryBatchInfoDO.getPageSize(), getDeviceObdExFactoryBatchInfoDO.getSortRow() + getDeviceObdExFactoryBatchInfoDO.getSortOrder());
        return deviceObdExFactoryBatchInfoMapper.selectList(getDeviceObdExFactoryBatchInfoDO);
    }

    public List<DeviceObdExFactoryBatchInfo> getAllList(GetDeviceObdExFactoryBatchInfoDO getDeviceObdExFactoryBatchInfoDO) {
        PageHelper.clearPage();
        PageHelper.orderBy(getDeviceObdExFactoryBatchInfoDO.getSortRow() + getDeviceObdExFactoryBatchInfoDO.getSortOrder());
        return deviceObdExFactoryBatchInfoMapper.selectList(getDeviceObdExFactoryBatchInfoDO);
    }

    public int save(DeviceObdExFactoryBatchInfo batchInfo) {
        batchInfo.setCreateTime(new Date());
        return deviceObdExFactoryBatchInfoMapper.insertSelective(batchInfo);
    }

    public DeviceObdExFactoryBatchInfo getById(long batchId) {
        return deviceObdExFactoryBatchInfoMapper.selectByPrimaryKey(batchId);
    }

    public int update(DeviceObdExFactoryBatchInfo batchInfo) {
        batchInfo.setLastModifiedTime(new Date());
        return deviceObdExFactoryBatchInfoMapper.updateByPrimaryKeySelective(batchInfo);
    }

    @Transactional
    public int updateExFactory(DeviceObdExFactoryBatchInfo batchInfo) {
        long factoryUserId = 0;
        int num = 0;
        boolean flag = true;
        flag = false;
        Date serviceStartTime = new Date();
        int serviceYear = batchInfo.getServiceYear();
        Date serviceEndTime = DateUtils.addDate(serviceStartTime,serviceYear,0,0,0,0,0,0);
        GetDeviceObdBaseInfoDO getDeviceObdBaseInfoDO = new GetDeviceObdBaseInfoDO();
        getDeviceObdBaseInfoDO.setBatchId(batchInfo.getBatchId());
        getDeviceObdBaseInfoDO.setSortRow("id");
        List<DeviceObdBaseInfo> list = new ArrayList<>();
        List<DeviceObdBaseInfo> baseList = deviceObdBaseInfoService.getAllList(getDeviceObdBaseInfoDO);
        if (baseList != null && baseList.size() > 0) {
            //如果关联的生厂商家允许二次供货，则设备先不关联厂家
            if(batchInfo.getFactoryUserId()!=null && batchInfo.getFactoryUserId()>0){
                UserInfo userInfo = userInfoService.getById(batchInfo.getFactoryUserId());
                if(userInfo!=null && userInfo.getAllowSecondSupply()!=null && userInfo.getAllowSecondSupply()==0){
                    factoryUserId = batchInfo.getFactoryUserId();
                }
            }
            for (DeviceObdBaseInfo info : baseList) {
                info.setSetGps(batchInfo.getSendTime());
                if (batchInfo.getSendTime() > 5) {
                    info.setSetGpsStatus(1);
                    info.setSetGpsSwitch(1);
                }
                //如果厂家是沃得或道依茨法尔，服务时间按出厂时间来计算
                if(info.getFactoryUserId()!= null && (info.getFactoryUserId() == 733 || info.getFactoryUserId()==732)) {
                    info.setServiceStartTime(serviceStartTime);
                    info.setServiceEndTime(serviceEndTime);
                }
                info.setLastModifiedTime(new Date());
                if(factoryUserId>0){
                    info.setFactoryUserId(factoryUserId);
                }
                if(batchInfo.getDomainId()!=null && batchInfo.getDomainId()!=0){
                    info.setDomainId(batchInfo.getDomainId());
                }
                list.add(info);
            }
            deviceObdBaseInfoService.updateList(list);
            flag = true;
        }
        if (flag) {
            batchInfo.setLastModifiedTime(new Date());
            num = deviceObdExFactoryBatchInfoMapper.updateByPrimaryKeySelective(batchInfo);
        }
        return num;
    }
}
